<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: ResourceopController.php 310 2011-09-18 04:55:22Z beimuaihui@gmail.com $
 */



class System_PermissionController extends Baogg_Controller_Action {

	function init() {
		Zend_Loader::loadClass ( 'Permission' );

	}

	/**
	 * The default action
	 */
	public function listAction() {
		global $LANG;

		BAOGG_LANGUAGE::loadFile("resource");

		$params=$this->getRequest()->getParams();
		/* $subject_url=$params;
		$subject_url['action']='data';
		$subject_url['resource_id']=$params['subject_id']; */
		
		$resource_url=$params;
		$resource_url['action']='data';
		
		
		
		$edit_url=$params;
		$edit_url['action']='edit';
		
		$Model=new Model();
		
		//get subject result
		$Resource=new Resource();
		$subject_id=$params['subject_id'];
		$rs_subject=$Resource->getOne($subject_id);
		$this->view->subject_name=$rs_subject[0]['resource_name'];
		
		
		
		
		//$Resource=new Resource();
		$resource_id=$params['resource_id'];
		$rs_resource=$Resource->getOne($resource_id);
		$this->view->resource_name=$rs_resource[0]['resource_name'];
		
		$this->view->LANG=$LANG;

		/* $OpinResource=new OpInResource();
		$rs_op_in_resource=$OpinResource->getList(array('resource_id'=>$resource_id));
		$arr_op_in_resource=array();
		foreach((array)$rs_op_in_resource as $v){			
			$arr_op_in_resource[]=$v['resource_row_id'].'_'.$v['resource_op_id'];		
			
		}
		$this->view->arr_op_in_resource=$arr_op_in_resource;
		 */
		$object_url=null;$object_name=null;
		if(isset($params['object_id'])){
			/* $object_url=$params;
			$object_url['action']='data';
			$object_url['resource_id']=$params['object_id']; */
			$rs_resource=$Resource->getOne($resource_id);
			$object_name=$rs_resource[0]['resource_name'];
		}
		$this->view->object_name=$object_name;
		
		
		
		//$this->view->subject_url=Baogg_Controller_Url::generate($subject_url);		
		$this->view->resource_url=Baogg_Controller_Url::generate($resource_url);
		//$this->view->object_url=$object_url?Baogg_Controller_Url::generate($object_url):$object_url;
		$this->view->edit_url=Baogg_Controller_Url::generate($edit_url);
		//echo '<pre>';var_dump($LANG);exit;
		
		
		$Resource=new Resource();
		$rs_subject=$Resource->getList(array('resource_code'=>'user'));
		$col_subject_id=@$rs_subject[0]['resource_id'];
		$rs_resource=$Resource->getList(array('resource_code'=>'role'));
		$col_resource_id= @$rs_resource[0]['resource_id'];
		$is_user_group_role=($subject_id==$col_subject_id && $resource_id==$col_resource_id);
		if($is_user_group_role){
			$Permission=new Permission();
			$UserGroupRole=new UserGroupRole();
			$rs_user_group_role=$UserGroupRole->getList();
			foreach((array)$rs_user_group_role as $v){
				$row_permisssion=array('subject_id'=>$col_subject_id,'subject_row_id'=>$v['user_id'],'resource_id'=>$col_resource_id,'resource_row_id'=>$v['role_id']);
				if(!($rs_permission=$Permission->getSame2($row_permisssion,' and '))){
					$Permission->_insert($row_permisssion);
				}
			}
		}


	}

	/**
	 * The data action
	 */
	function dataAction() {
		$post=$this->_request->getPost ();
		$params=$this->getRequest()->getParams();
		//echo '<pre>';print_r($this->getRequest()->getParams());
		$resource_type=@$params['resource_type'];
		$resource_id=@$params[$resource_type];
		
		$Resource=new Resource();
		$rs_resource=$Resource->getOne($resource_id);
		$filter=$rs_resource[0]['filter'];
		$sorting=$rs_resource[0]['sorting'];
		$arr_sorting=explode(" ",trim($sorting));
		$sort=null;
		if(count($arr_sorting)>=1){
			//
			$arr_sorting=array_values(array_filter($arr_sorting));
			if($arr_sorting){
				$sort=array('sort'=>$arr_sorting[0],'dir'=>(count($arr_sorting)>=2?$arr_sorting[1]:'asc'));
			}
		}
	
		//get resource op ,and related filter
		$where=array();
		$OpInResource=new OpInResource();
		$ResourceOp=new ResourceOp();
		$rs_op_in_resource2=array();
		$rs_resource_op=$ResourceOp->getList(array('resource_id'=>(int)$resource_id,'resource_op_pid'=>0),array('sort'=>'ordering','dir'=>'asc'),null,array('id'=>'resource_op_id','text'=>'resource_op_name'));
		$where['resource_row_id']=array();
		foreach((array)$rs_resource_op as $k_op=>$v_op){			
			$rs_op_in_resource=$OpInResource->getList(array('resource_id'=>(int)$resource_id,'resource_op_id'=>(int)$v_op['id']));
			//echo '<pre>';print_r($rs_op_in_resource);exit;
			foreach((array)$rs_op_in_resource as $k=>$v){
				$where['resource_row_id'][$v['resource_row_id']]=$v['resource_row_id'];
				$id=$v['resource_row_id'].'_'.$v['resource_op_id'];
				$rs_op_in_resource2[$id]['id']=$id;
				$rs_op_in_resource2[$id]['text']=$v_op['text'];
				$rs_op_in_resource2[$id]['cls']='file';
				$rs_op_in_resource2[$id]['leaf']=true;
				$rs_op_in_resource2[$id]['checked']=false;
				$rs_op_in_resource2[$id]['pid']=$v['resource_row_id'];
			}
		}
		
		
		
		$Model=new Model();
		$rs_model_detail=$Model->getModelAndField($rs_resource[0]['model_id']);
		extract($rs_model_detail);
		
		if($where['resource_row_id']){
			$filter=array($filter,$key_col=>array_values($where['resource_row_id']));
		}
		
		$instance=new $class_name();
		
		$cols=array('id'=>$key_col,'text'=>$label_col);
		if(isset($parent_col)){
			$cols['pid']=$parent_col;
		}
		
		$arr_selected=array();
		if($subject_row_id=$this->getRequest()->getParam('subject_row_id')){
			if($resource_type=='subject_id'){
				$arr_selected=$subject_row_id;
			}else if($resource_type=='resource_id'){
				$Permission=new Permission();				
				$rs_permission=$Permission->getList($params);
				foreach((array)$rs_permission as $v){
					$arr_selected[]=$v['resource_row_id'];
					if($rs_op_in_resource2){
						$arr_selected[]=$v['resource_row_id'].'_'.$v['resource_op_id'];;
					}
				}
			}else if($resource_type=='subject_id'){
				$Permission=new Permission();				
				$rs_permission=$Permission->getList($params);
				foreach((array)$rs_permission as $v){
					$arr_selected[]=$v['subject_row_id'];
				}
			}	
		}
		//echo '<pre>';print_r($arr_selected);exit;
		
		$rs=$instance->getExtTree($filter,$sort,null,$cols,null,$rs_op_in_resource2,$arr_selected);//
		//echo '<pre>';print_r($rs);exit;
		$rs=array(array('id'=>'0','text'=>$rs_resource[0]['resource_name'],'checked'=>false,'expanded'=>false, 'children'=>$rs));
	
		
		
		
		echo Zend_Json::encode($rs);
		$this->_helper->viewRenderer->setNoRender ();
	}
	
	

	/**
	 * The add action
	 */
	function addAction(){
		//$this->saveAction();
	}

	
	/**
	 * The delete action
	 */
	function deleteAction() {

		$ret=array("success"=>false,"msg"=>"delete_fail");
		$id = Baogg_Controller_Action::_getParam ( 'ids' );
			
		 

		if ($this->resourceOp->_delete ( $id )){
			$ret['success'] = true;
			$ret["msg"]="delete_ok";
		}

		echo Baogg_Language::outputResult($ret);
		$this->_helper->viewRenderer->setNoRender ();

	}

	function removeAction(){
		$this->deleteAction();
	}

	/**
	 * The save action
	 */
	function editAction() {
		//global $LANG;

		$ret=array("success"=>false,"msg"=>"save_fail");

		// echo '<pre>';print_r($this->resourceOp->info(ResourceOp::METADATA));exit;
		//check form
		if (!$this->_request->isPost ())
		{
			echo Baogg_Language::outputResult($ret);
			exit;
		}

		$formData = $this->_request->getPost ();

		if(!$formData)
		{
			echo Baogg_Language::outputResult($ret);
			exit;
		}
		
		//echo '<pre>';print_r($formData);exit;
		
		$subject_row_ids=@$formData['subject_row_id'];
		$resource_row_ids=@$formData['resource_row_id'];
		
		
		$arr_subject_row_id=array_filter(explode(',',$subject_row_ids));
		$arr_resource_row_id=array_filter(explode(',',$resource_row_ids));
		$subject_id=$this->getRequest()->getParam('subject_id');
		$resource_id=$this->getRequest()->getParam('resource_id');
		
		//echo '<pre>';var_dump($subject_id);var_dump($resource_id);exit;
		
		if(!$arr_subject_row_id || !$arr_resource_row_id || !$subject_id || !$resource_id){
			echo Baogg_Language::outputResult($ret);
			exit;
		}
		
		$ResourceOp=new ResourceOp();
		$rs_resource_op=$ResourceOp->getList(array('resource_id'=>(int)$resource_id));
		$is_exist_op=$rs_resource_op?true:false;
		
		
		//import to permission from user_group_role
		/* $Resource=new Resource();
		$rs_subject=$Resource->getList(array('resource_code'=>'user'));
		$col_subject_id=@$rs_subject[0]['resource_id'];
		$rs_resource=$Resource->getList(array('resource_code'=>'role'));
		$col_resource_id= @$rs_resource[0]['resource_id'];
		$is_user_group_role=($subject_id==$col_subject_id && $resource_id==$col_resource_id);
		if($is_user_group_role){
			$Permission=new Permission();
			$UserGroupRole=new UserGroupRole();
			$rs_user_group_role=$UserGroupRole->getList();			
			foreach((array)$rs_user_group_role as $v){
				$row_permisssion=array('subject_id'=>$col_subject_id,'subject_row_id'=>$v['user_id'],'resource_id'=>$col_resource_id,'resource_row_id'=>$v['role_id']);
				if(!($rs_permission=$Permission->getSame2($row_permisssion,' and '))){
					$Permission->_insert($row_permisssion);
				}
			}	
		} */
		
		//first delete all
		$Permission =new Permission();
		$Permission->delete(array('subject_id='.$subject_id, ' resource_id = '.$resource_id,'subject_row_id in ('.implode(',',$arr_subject_row_id).')'));
		
		
		foreach((array)$arr_subject_row_id as $v_subject_row_id){
			foreach((array)$arr_resource_row_id as $v_resource_row_id){
				$row=array('subject_id'=>(int)$subject_id,'resource_id'=>(int)$resource_id);
				$row['subject_row_id']=$v_subject_row_id;
				if(!$is_exist_op){
					$row['resource_row_id']=$v_resource_row_id;
				}else{
					$arr_v_resource_row_id=explode("_",$v_resource_row_id);
					if(count($arr_v_resource_row_id)!=2){
						continue;
					}
					$row['resource_row_id']=$arr_v_resource_row_id[0];
					$row['resource_op_id']=$arr_v_resource_row_id[1];
				}
				$Permission->_insert($row);
			}
		}
		
		
		
		$Resource=new Resource();
		$rs_subject=$Resource->getList(array('resource_code'=>'user'));
		$col_subject_id=@$rs_subject[0]['resource_id'];
		$rs_resource=$Resource->getList(array('resource_code'=>'role'));
		$col_resource_id= @$rs_resource[0]['resource_id'];
		$is_user_group_role=($subject_id==$col_subject_id && $resource_id==$col_resource_id);
		if($is_user_group_role){
			$Role=new Role();
			$UserGroupRole=new UserGroupRole();
			$UserGroupRole->delete(array('user_id in ('.implode(',',$arr_subject_row_id).')'));
			
			$rs_user_group_role=$Permission->getList(array('subject_id='.$subject_id, ' resource_id = '.$resource_id,'subject_row_id in ('.implode(',',$arr_subject_row_id).')'));
			foreach((array)$rs_user_group_role as $v){
				$row_user_group_role=array('user_id'=>$v['subject_row_id'],'role_id'=>$v['resource_row_id']);
				$rs_role=$Role->getOne($v['resource_row_id']);
				$row_user_group_role['user_group_id']=$rs_role[0]['is_user_default']=='2'?$rs_role[0]['user_group_id']:0;
				$UserGroupRole->_insert($row_user_group_role);
			}
		}
				
		$ret=array("success"=>true,"msg"=>"save_ok");


		//$db=Zend_Registry::get("db");echo Baogg_Db::debugDb($db);exit;
		echo Baogg_Language::outputResult($ret);

		
		
		
		
		$this->_helper->viewRenderer->setNoRender ();


	}

}
